/**
 * layui模板引入
 * @type {{}}
 */
window.LAYUI_TEMPLET = function () {
    return {
        /**
         * 生成表格上方按钮
         * @param list [add,del]
         * @returns {string} 返回组装的按钮HTML
         */
        getToolbarHtml: function(list) {
            let barBtnTemp = {
                add:
                    '<button class="pear-btn pear-btn-primary pear-btn-md mar-rgt-7" lay-event="add" >'+
                    '   <i class="layui-icon layui-icon-add-1"></i>'+
                    '   新增'+
                    '</button>',
                del:
                    '<button class="pear-btn pear-btn-danger pear-btn-md mar-rgt-7" lay-event="batchRemove">'+
                    '   <i class="layui-icon layui-icon-delete"></i>'+
                    '   删除'+
                    '</button>',
                enable:
                    '<button class="pear-btn pear-btn-danger pear-btn-md mar-rgt-7" lay-event="enable">'+
                    '   <i class="layui-icon layui-icon-enable"></i>'+
                    '   启用'+
                    '</button>',
                disable:
                    '<button class="pear-btn pear-btn-danger pear-btn-md mar-rgt-7" lay-event="disable">'+
                    '   <i class="layui-icon layui-icon-disable"></i>'+
                    '   禁用'+
                    '</button>',
            };
            return  '<div class="layui-table-tool-temp">' + (list || []).map(i => barBtnTemp[i]).join('') + '</div>';
        },
        /**
         * 生成表格行操作按钮
         * @param list [add,edit,del...]
         * @returns {string} 生成的HTML
         */
        getTableHandleHtml: function(list) {
            let rowOperatingTemp = {
                edit: '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-orange" lay-event="edit">编辑</a>',
                remove: '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-red" lay-event="remove">删除</a>',
                discard: '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-red" lay-event="discard">废弃</a>',
                show: '<a class="layui-btn layui-btn-xs  layui-btn-primary layui-border-black" lay-event="show">查看</a>',
                permission: '<a class="layui-btn layui-btn-xs layui-btn-primary layui-border-green" lay-event="permission">权限分配</a>',
            };
            return  '<div class="layui-table-tool-temp">' + (list || []).map(i => rowOperatingTemp[i]).join('') + '</div>';
        },
        /**
         * 数字转汉字
         */
        numberToChinese: function(d) {
            let val = d[this['field']];
            let chinese = ['零','一','二','三','四','五','六','七','八','九'];
            return chinese[val];
        },
        /**
         * 布尔转汉字
         */
        boolToChinese: function(d) {
            return d[this['field']] ? '是' : '否';
        },
        /**
         * 行序号
         */
        row_no: function(d) {
            return d.LAY_INDEX;
        },
        /**
         * 时间格式化
         */
        date_format: function(d) {
            if(!d[this['field']]){
                return  ''
            }
            return layui.util.toDateString(d[this['field']], 'yyyy-MM-dd HH:mm:ss');
        },

        /**
         * form表单html生成
         *  type: select,number,datetime,upload,choose_menu,text
         *  例子：
             {type: 'hidden', name: 'pid', value: "1"},
             {type: 'text', label: '名称', name: 'username', value: "zhangsan", verify: "required"},
             {type: 'number', label: '排序', name: 'sort'},
             {type: 'datetime', label: '日期', name: 'bdate', value: "", format: "date", verify: "required", range: true}, //year ,month, time, datetime
             {type: 'radio', label: '预算', name: 'budget',
                 values: [{value: '1', text: '是'}, {checked: true, value: '0', text: '否'}]
             },
             {type: 'checkbox', name: 'like', label: '爱好',
                 values: [{checked: true, value: '1', text: '跳舞'}, {value: '0', text: '唱歌'}]
             },
             {type: 'defaultSelect', name: 'pmenu', label: '父菜单', verify: "required",
                 options: [{ value: '', text: '请选择父菜单', selected: true}, { value: '1', text: '用户管理'}, {value: '2', text: '权限管理'}, {value: '3', text: '资产管理'}]
             },
             {type: 'singleSelect', label: '部门负责人', name: 'leader', verify: "", url: g.ctx + '/api/sys/user/list',  echo: g.ctx + '/api/sys/user/get', value: ''},
             {type: 'multipleSelect', label: '监管部门', name: 'partDept', verify: "", url: g.ctx + '/api/sys/dept/list', echo: g.ctx + '/api/sys/dept/get', value: ''},
             {type: 'treeSelect', name: 'demoTree', value: "-1,14,5,6",label: '树形菜单', verify: "required", url: '../../static/plugins/admin/data/xmTree.json'},
             {type: 'textarea', label: '描述', name: 'desc', text: "描述内容", verify: "required", colspan: 12},
         */
        formHtmlGen: function(formData) {
            formData = formData || [];
            let htmlList = [];
            formData.forEach(function (item) {
                let itemHtml = '';
                item['value'] = item.value ? item.value : "";               // 默认值
                item['verify'] = item.verify ? item.verify : "";            // input验证属性
                item['readonly'] = item.readonly ? item.readonly : false;   // 只读
                item['required'] = item.verify.split("|").indexOf("required") >= 0;

                switch (item.type) {
                    case 'hidden':
                        itemHtml = '' +
                            '<input type="hidden" value="'+ item.value +'" name="'+ item.name +'" />';
                        break;
                    case 'text':
                        itemHtml += '' +
                            '<input ' +
                            '   type="text"'+
                                (item.readonly ? 'readonly' : '') +
                            '   value="'+ item.value + '" ' +
                            '   name="'+ item.name +'"' +
                            '   autocomplete="off"' +
                            '   placeholder="请输入'+ item.label +'"' +
                            '   lay-verify="'+ item.verify +'"' +
                            '   lay-reqText="'+ item.label +'不能为空"' +
                            '   class="layui-input">';
                        break;
                    case 'textarea':
                        let text = item.text === undefined ? "" : item.text;
                        itemHtml = '<textarea ' +
                            '   name="'+ item.name +'"' +
                            '   placeholder="请输入'+item.label+'" ' +
                            '   lay-verify="'+item.verify+'"  ' +
                            '   lay-reqText="'+ item.label +'不能为空"' +
                            '   class="layui-textarea">'+text+'</textarea>';
                        break;
                    case 'number':
                        itemHtml += '' +
                            '<input type="text" ' +
                                (item.readonly ? 'readonly' : '') +
                            '   value="'+ item.value + '" ' +
                            '   oninput="this.value=this.value.replace(/[^\\0-9\\.]/g,\'\')"' +
                            '   name="'+ item.name +'"' +
                            '   autocomplete="off"' +
                            '   placeholder="请输入'+ item.label +'"' +
                            '   lay-verify="'+ item.verify +'"' +
                            '   lay-reqText="'+ item.label +'不能为空"' +
                            '   class="layui-input">';
                        break;
                    case 'datetime':
                        itemHtml += '<input class="layui-input layui-date" type="text" ' +
                            '   id="'+ item.name +'" ' +
                                (item.readonly ? 'readonly' : '') +
                            '   name="'+ item.name +'" ' +
                            '   value="'+ item.value +'" ' +
                            '   format="'+ item.format +'" ' +
                            '   range="'+ !!item.range +'" ' +
                            '   placeholder="请输入'+ item.label +'"' +
                            '   lay-verify="'+ item.verify +'"' +
                            '   lay-reqText="'+ item.label +'不能为空"' +
                            '   autocomplete="off">';
                        break;
                    case 'radio':
                        itemHtml += '' +
                            (item.values || []).map(function (i) {
                                return '<input type="radio" '+ (i['checked'] ? 'checked' : '') +' value="'+ i['value'] +'" name="'+ item.name +'" title="'+ i['text'] +'" />'
                            }).join('');
                        break;
                    case 'checkbox':
                        itemHtml += '' +
                            (item.values || []).map(function (i) {
                                return '<input lay-skin="primary" type="checkbox" '+ (i['checked'] ? 'checked' : '') +' value="'+ i['value'] +'" name="'+ item.name +'" title="'+ i['text'] +'" />'
                            }).join('');
                        break;
                    case 'defaultSelect':
                        let optionHtmlList = (item.options || []).map(function (o) {
                            return '<option '+ (o['selected'] ? 'selected' : '') +' value="'+ o.value +'">'+ o.text +'</option>'
                        });
                        itemHtml += '' +
                            '<select name="'+item.name+'"  lay-verify="'+item.verify+'"  lay-reqText="'+ item.label +'不能为空">' + optionHtmlList.join('') + '</select>';
                        break;
                    case 'singleSelect':
                        itemHtml += '<div id="'+item.name+'" class="single-select" data-label="'+item.label+'" data-verify="'+item.verify+'" data-name="'+item.name+'"  data-value="'+item.value+'" data-url="'+item.url+'" data-echo="'+item.echo+'"></div>';
                        break;
                    case 'multipleSelect':
                        itemHtml += '<div id="'+item.name+'" class="multiple-select" data-label="'+item.label+'" data-verify="'+item.verify+'" data-name="'+item.name+'" data-value="'+item.value+'"  data-url="'+item.url+'" data-echo="'+item.echo+'"></div>';
                        break;
                    case 'singleTreeSelect':
                        itemHtml += '<div id="'+item.name+'" class="single-tree-select" data-label="'+item.label+'" data-verify="'+item.verify+'" data-name="'+item.name+'" data-value="'+item.value+'"  data-url="'+item.url+'"></div>';
                        break;
                    case 'multipleTreeSelect':
                        itemHtml += '<div id="'+item.name+'" class="multiple-tree-select" data-label="'+item.label+'" data-verify="'+item.verify+'" data-name="'+item.name+'" data-value="'+item.value+'"  data-url="'+item.url+'"></div>';
                        break;
                    default:
                        itemHtml += '';
                        break;
                }

                if(item.type === 'deepTree'){
                    itemHtml = '' +
                        '    <div class="layui-col-sm'+ (item['colspan'] ? item['colspan'] : 6) +'">' +
                        '      <div class="layui-form-item '+ (item.required ? ' required ' : '') +'">' +
                        itemHtml +
                        '       </div>' +
                        '   </div>';
                }else {
                    if(item.type !== 'hidden'){
                        itemHtml = '' +
                            '    <div class="layui-col-sm'+ (item['colspan'] ? item['colspan'] : 6) +'">' +
                            '      <div class="layui-form-item '+ (item.required ? ' required ' : '') +'">' +
                            '           <label class="layui-form-label">'+ item.label +'</label>' +
                            '           <div class="layui-input-block">' +
                            itemHtml +
                            '           </div>' +
                            '        </div>' +
                            '   </div>';
                    }
                }

                htmlList.push(itemHtml);
            });

            return '' +
                '<form class="layui-form" id="layuiForm">' +
                '    <div class="mainBox" style="bottom: 0">' +
                '        <div class="main-container">' +
                '            <div class="layui-row">' +
                htmlList.join('') +
                '            </div>' +
                '        </div>' +
                '    </div>' +
                '    <div class="bottom layui-hide">' +
                '        <div class="button-container">' +
                '            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit lay-filter="layui-form">' +
                '                <i class="layui-icon layui-icon-ok"></i>' +
                '                提交' +
                '            </button>' +
                '            <button type="reset" class="pear-btn pear-btn-sm">' +
                '                <i class="layui-icon layui-icon-refresh"></i>' +
                '                重置' +
                '            </button>' +
                '        </div>' +
                '    </div>' +
                '</form>';
        },
    }
}();



